<template>
  <div class="directorys-container">
    <el-card>
      <!-- 面包屑 -->
      <div slot="header" v-if="subject.id && subject.name">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item>学科管理</el-breadcrumb-item>
          <el-breadcrumb-item>{{ subject.name }}</el-breadcrumb-item>
          <el-breadcrumb-item>目标管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form :inline="true" label-width="80px" size="small">
            <el-form-item label="目录名称">
              <el-input
                v-model="requestParams.directoryName"
                style="200px"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="requestParams.state">
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align: right">
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="filter()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="text"
            icon="el-icon-back"
            v-if="subject.id && subject.name"
            @click="$router.back()"
            >返回学科</el-button
          >
          <el-button
            icon="el-icon-edit"
            type="success"
            size="small"
            @click="openDialog()"
            >新增目录</el-button
          >
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共${total}条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px"
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="directorys">
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column label="所需学科" prop="subjectName"></el-table-column>
        <el-table-column
          label="目录名称"
          prop="directoryName"
        ></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期">
          <template #default='{row}'>{{
            row.addDate | parseTimeByString
          }}</template>
        </el-table-column>
        <el-table-column label="面试题数量" prop="totals"> </el-table-column>
        <el-table-column label="状态" prop="state">
          <template #default='{row}'>{{
            row.state === 1 ? "已启用" : "已禁用"

          }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template #default='{row}'>
            <el-button type="text" @click="toggleState(row)">{{
              row.state === 1 ? "禁用" : "启用"
            }}</el-button>
            <el-button
              type="text"
              @click="openDialog(row)"
              :disabled="row.state === 1"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="dele(row)"
              :disabled="row.state === 1 || row.totals > 0"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        :page-sizes="[5, 10, 20, 50]"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <!-- 新增功能 -->
    <DirectorysAdd
      ref="directorysAdd"
      :data="currDirectory"
      @newdataList="newdataList"
    >
    </DirectorysAdd>
  </div>
</template>
<script>
import DirectorysAdd from '../components/directorys-add'
import { list, changeState, remove } from '@/api/hmmm/directorys.js'
export default {
  name: 'DirectorysList',
  components: {
    DirectorysAdd
  },
  data () {
    return {
      requestParams: {
        directoryName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      directorys: [],
      total: 0,
      currDirectory: {}
    }
  },
  created () {
    this.getList()
  },
  computed: {
    subject () {
      return this.$route.query || {}
    }
  },
  methods: {
    // 查询列表
    async getList () {
      // this.requestParams.subjectID = this.subject.id || ''
      const { data } = await list(this.requestParams)
      console.log(data)
      this.directorys = data.items
      this.total = data.counts
    },
    openDialog (directory = {}) {
      this.currDirectory = directory
      this.$nextTick(() => {
        this.$refs.directorysAdd.open()
      })
    },
    // 清除输入框功能
    clear () {
      this.requestParams = {
        directoryName: '',
        state: '',
        page: 1,
        pagesize: 10
      }
    },
    // 进行筛选
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    // 切换状态
    async toggleState (directory) {
      await changeState({
        id: directory.id,
        state: directory.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      directory.state = directory.state === 1 ? 0 : 1
    },
    // 每页条数变化时
    handleSizeChange (val) {
      this.requestParams.pagesize = val
      // 规范: 每页条数变化时, 重置成第一页!!!
      // 一共100条 1条/页,  用户在第80页 => 合理
      // 一共100条 100条/页,  用户应该在第一页
      this.requestParams.page = 1

      this.getList()

      // console.log(`每页 ${val} 条`)
    },
    // 页码变化时,
    handleCurrentChange (val) {
      this.requestParams.page = val
      this.getList()
      // console.log(`当前页: ${val}`)
    },
    // 新增/修改
    newdataList () {
      if (!this.currDirectory.id) {
        this.requestParams.page = 1
      }
      this.getList()
    },
    // 删除
    dele (directory) {
      this.$confirm('确定删除改信息吗？', '温馨提示').then(async () => {
        await remove(directory)
        this.$message.success('删除成功')
        this.getList()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.directorys-container {
  padding: 10px;
}
</style>
